package com.vhall.component.entity.chat.vo;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.vhall.component.entity.paas.PaasMessageListRspDTO;
import com.vhall.component.entity.room.dto.RoomJoinsEntityDTO;
import com.vhall.component.framework.common.constant.CommonConstant;
import com.vhall.component.plugin.common.constant.RoomRoleNameCons;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Objects;

/**
 * @author yuanzh
 * @date 2021/9/24
 */
@Data
@Accessors(chain = true)
public class ChatListRspVo2 implements Serializable {

    private static final long serialVersionUID = 4592307976142698222L;

    /**
     * 头像
     */
    private String avatar;

    /**
     * 频道id
     */
    @JSONField(name = "channel_id")
    private String channelId;

    /**
     * 消息内容
     */
    private JSONObject context;

    /**
     * 发送消息时间
     */
    @JSONField(name = "date_time")
    private LocalDateTime dateTime;

    /**
     * 消息id
     */
    @JSONField(name = "msg_id")
    private String msgId;

    /**
     * 昵称
     */
    @JSONField(name = "nick_name")
    private String nickname;

    /**
     * 角色
     */
    @JSONField(name = "role_name")
    private String roleName;

    /**
     * 发送者id
     */
    @JSONField(name = "sender_id")
    private String senderId;

    /**
     * 消息时间 展示
     */
    private String showTime;

    @JSONField(name = "data")
    private MessageData messageData;

    /**
     * 聊天消息
     */
    @Data
    public static class MessageData {

        /**
         * 消息内容
         */
        @JSONField(name = "text_content")
        private String textContent;

        /**
         * 消息类型
         */
        private String type;

        /**
         * 图片地址
         */
        @JSONField(name = "image_urls")
        private JSONArray imageUrls;

    }

    public static class Builder {

        private final ChatListRspVo2 rspVo;

        public Builder(PaasMessageListRspDTO.PaasChatRecord chatRecord) {
            this.rspVo = new ChatListRspVo2();

            this.rspVo.setMsgId(chatRecord.getMsgId());
            this.rspVo.setDateTime(
                    DateUtil.parse(chatRecord.getDateTime(), CommonConstant.DATETIME_FORMAT).toInstant().atZone(ZoneId.systemDefault())
                            .toLocalDateTime());

            // 发送者信息
            this.rspVo.setSenderId(chatRecord.getThirdPartyUserId());
            this.rspVo.setNickname(chatRecord.getNickname());
            this.rspVo.setAvatar(chatRecord.getAvatar());
            // 消息内容初始化
            MessageData messageData = new MessageData();
            messageData.setTextContent(chatRecord.getData());
            messageData.setType(chatRecord.getType());
            messageData.setImageUrls(chatRecord.getImageUrls());
            this.rspVo.setMessageData(messageData);

            JSONObject context = chatRecord.getContext();
            try {
                JSONArray roleNameText = context.getJSONArray("roleNameText");
                if (roleNameText != null) {
                    for (Object o : roleNameText) {
                        JSONObject roleName = (JSONObject) o;
                        if (!RoomRoleNameCons.ROLE_TYPE_STR.contains(roleName.getString("type"))
                                || !RoomRoleNameCons.ROLE_TYPE_NAME.contains(roleName.getString("text"))) {
                            roleNameText.remove("type");
                            roleNameText.remove("text");
                            break;
                        }
                    }
                }
                context.put("roleNameText", roleNameText);
            } catch (ClassCastException e) {
                JSONObject object = context.getJSONObject("roleNameText");
                if (object != null) {
                    if (!RoomRoleNameCons.ROLE_TYPE_STR.contains(object.getString("type"))
                            || !RoomRoleNameCons.ROLE_TYPE_NAME.contains(object.getString("text"))) {
                        object.remove("type");
                        object.remove("text");
                    }
                }
                context.put("roleNameText", object);
            }
            this.rspVo.setContext(context);
        }

        public ChatListRspVo2 build() {
            return this.rspVo;
        }

        public ChatListRspVo2.Builder initUserInfo(RoomJoinsEntityDTO roomJoinsDTO) {
            if (Objects.nonNull(roomJoinsDTO)) {
                this.rspVo.setNickname(StringUtils.isBlank(roomJoinsDTO.getNickname()) ? "" : roomJoinsDTO.getNickname());
                this.rspVo.setAvatar(StringUtils.isBlank(roomJoinsDTO.getAvatar()) ? "" : roomJoinsDTO.getAvatar());
                this.rspVo.setRoleName(StringUtils.isBlank(roomJoinsDTO.getRoleName()) ? "" : roomJoinsDTO.getRoleName());
            }
            return this;
        }
    }
}
